package ael.imarkket;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ShoppingListAdapter extends BaseAdapter {
	private final Activity		actividad;
	private final String[][]	MatrizBidimensionalJSON;
	private final int			CountTotal;

	public ShoppingListAdapter(Activity actividad, int Total, String[][] pMatrizBidimensionalJSON) {
		super();
		this.CountTotal = Total;
		this.actividad = actividad;
		this.MatrizBidimensionalJSON = pMatrizBidimensionalJSON;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = actividad.getLayoutInflater();

		// Hace referencia al fichero xml shopping_list_item.xml
		View view = inflater.inflate(R.layout.shoppinglistitem, null, true);

		// String JSON_OBJECT_ID = this.MatrizBidimensionalJSON[position][0];
		String JSON_OBJECT_DESCRIPTION = this.MatrizBidimensionalJSON[position][1];
		String JSON_OBJECT_CREATED = "CR: " + this.MatrizBidimensionalJSON[position][2];
		String JSON_OBJECT_UPDATED = "UP: " + this.MatrizBidimensionalJSON[position][3];
		int JSON_OBJECT_SHARED = Integer.parseInt(this.MatrizBidimensionalJSON[position][4]);
		int JSON_OBJECT_COUNT = Integer.parseInt(this.MatrizBidimensionalJSON[position][5]);

		TextView description = (TextView) view.findViewById(R.id.titulo);
		description.setText(JSON_OBJECT_DESCRIPTION);

		TextView created = (TextView) view.findViewById(R.id.created);
		created.setText(JSON_OBJECT_CREATED);

		TextView updated = (TextView) view.findViewById(R.id.updated);
		updated.setText(JSON_OBJECT_UPDATED);

		TextView elements = (TextView) view.findViewById(R.id.elements);
		elements.setText(Integer.toString(JSON_OBJECT_COUNT));

		ImageView shared = (ImageView) view.findViewById(R.id.shared);
		if (JSON_OBJECT_SHARED == 1)
			shared.setImageResource(R.drawable.multiusers);
		else
			shared.setImageResource(R.drawable.user);

		// Imágenes según la cantidad de elementos
		ImageView imageicon = (ImageView) view.findViewById(R.id.icono);
		if (JSON_OBJECT_COUNT > 0)
			imageicon.setImageResource(R.drawable.basketfull);
		else
			imageicon.setImageResource(R.drawable.basketempty);

		return view;
	}

	public int getCount() {
		return this.CountTotal;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
}